﻿using ModelDesignEduse.Framework;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WorkbenchTest
{
    public class PageTypeEditor : EditorType
    {
        public PageTypeEditor()
        {
            Selectors.Add(new PrimaryKeySelector("Şablon ID"));
            Selectors.Add(new StringSelector("Şablon İsmi"));
            Selectors.Add(new StringSelector("Açıklama"));
            Selectors.Add(new PageTypeCategorySelector());
        }

        public override List<List<string>> Apply(List<List<string>> row_values)
        {
            List<List<string>> newRows = new List<List<string>>(row_values.Count);

            try
            {
                foreach (var row in row_values)
                {
                    CRUD.ExecuteCommandNoReturn("call add_page_type(" + (row[0] == null ? "0" : row[0])
                            + " , '" + row[1] + "' , '" + row[2] + "' , " + row[3] + ");");
                }

                var reader = CRUD.ExecuteCommandList("call get_recent_page_types(" + row_values.Count + ");");

                while (reader.Read())
                {
                    List<string> newValues = new List<string>();
                    
                    newValues.Add(reader.GetInt32("page_type_id").ToString());
                    newValues.Add(reader["page_type_name"] as string);
                    newValues.Add(reader["page_type_desc"] as string);
                    newValues.Add(reader.GetInt32("page_type_category_id").ToString());

                    newRows.Add(newValues);
                }

                newRows.Reverse();

                reader.Close();
            }
            catch (MySqlException me)
            {
                MessageBox.Show(me.Message, "MySQL Hatası", MessageBoxButtons.OK,
                    MessageBoxIcon.Error);

                return row_values;
            }

            return newRows;
        }

        public override List<List<string>> GetAll()
        {
            List<List<string>> allRows = new List<List<string>>();

            var reader = CRUD.ExecuteCommandList("call get_all_page_types();");

            while (reader.Read())
            {
                List<string> newValues = new List<string>();

                newValues.Add(reader.GetInt32("page_type_id").ToString());
                newValues.Add(reader["page_type_name"] as string);
                newValues.Add(reader["page_type_desc"] as string);
                newValues.Add(reader.GetInt32("page_type_category_id").ToString());

                allRows.Add(newValues);
            }

            reader.Close();

            return allRows;
        }

        public override void Delete(List<List<string>> row_values)
        {
            foreach (var row in row_values)
                CRUD.ExecuteCommandNoReturn("call delete_page_type(" + (row[0] == null ? "0" : row[0]) + ");");
        }
    }
}
